﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    sql=new mySql;
    sql->openbase();
    QString tableName="weathe";
    if(!sql->isExist(tableName))
    {
    sql->createTable();
    }
    tableName="cityCode";
    sql->dropTable(tableName);
    if(!sql->isExist(tableName))
    {
        sql->createCityTab();
    }
    QFile fp(QString("D:\\QT\\QTproject\\practice_0626\\try_1\\city.txt"));
    int i=0;
    if(fp.open(QIODevice::ReadOnly|QIODevice::Text))
     {
        qDebug("open success");
        QTextStream allCity(&fp);
        allCity.setCodec("UTF-8");
        while(!allCity.atEnd())
        {
        QString line=allCity.readLine();
        QStringList parts = line.split(QChar('\t'));
        if (parts.size() == 2) {
         city data;
         data.code = parts.at(0);
         data.name = parts.at(1);
         if(i<5)
         {
                qDebug()<<data.name;
                i++;
         }
         sql->addCity(data);
        }
     }
        fp.close();
    }
    else
    {
        qDebug("打开失败");
    }
    net=new QNetworkAccessManager(this);
    connect(net,&QNetworkAccessManager::finished,this,&MainWindow::on_net_finished);
    ui->lineEdit->setPlaceholderText("请输入你要查找的城市名");
    model=new QStandardItemModel(ui->tableView);
    ui->tableView->setModel(model);
   // delegate=new myDelegate;
   // ui->tableView->setItemDelegateForColumn(0,delegate);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    //QString city=ui->lineEdit->text();
    sql->findData(ui->lineEdit->text());
    QString addr="http://t.weather.sojson.com/api/weather/city/";
    addr+="101250401";
    qDebug()<<addr;
    net->get(QNetworkRequest(QUrl(addr)));
}

void MainWindow::on_net_finished(QNetworkReply *reply)
{
    int code=reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    if(code!=200)
    {
        qDebug()<<reply->errorString();
    }
    else
    {
        QByteArray json=reply->readAll();
        parse_json(json);
    }
}

void MainWindow::parse_json(QByteArray json)
{   QString time;
    QString city;
    QString quality;
    QString wendu;
    forecast *yesterday;
    int clumns=0;
    QVector<forecast*> vect;
    QJsonParseError json_error;
    QJsonDocument json_doc=QJsonDocument::fromJson(json,&json_error);
    if(json_error.error==QJsonParseError::NoError)
    {
        if(json_doc.isObject())
        {
            QJsonObject json_object_1=json_doc.object();
            if(json_object_1.contains("time"))
            {
                QJsonValue json_value_1=json_object_1.value("time");
                if(json_value_1.isString())
                {
                    time=json_value_1.toString();
                }
            }
            if(json_object_1.contains("cityInfo"))
            {
                QJsonValue json_value_2=json_object_1.value("cityInfo");
                if(json_value_2.isObject())
                {
                    QJsonObject json_object_2=json_value_2.toObject();
                    if(json_object_2.contains("city"))
                    {
                        QJsonValue json_value_3=json_object_2.value("city");
                        if(json_value_3.isString())
                        {
                            city=json_value_3.toString();
                        }
                    }
                }
            }
            if(json_object_1.contains("data"))
            {
                QJsonValue json_value_4=json_object_1.value("data");
                if(json_value_4.isObject())
                {
                    QJsonObject json_object_3=json_value_4.toObject();
                    if(json_object_3.contains("quality"))
                    {
                        QJsonValue json_value_5=json_object_3.value("quality");
                        if(json_value_5.isString())
                        {
                            quality=json_value_5.toString();
                        }
                    }
                    if(json_object_3.contains("wendu"))
                    {
                        QJsonValue json_value_6=json_object_3.value("wendu");
                        if(json_value_6.isString())
                        {
                            wendu=json_value_6.toString();
                        }
                    }
                    if(json_object_3.contains("yesterday"))
                    {
                        QJsonObject object=json_object_3.value("yesterday").toObject();
                        QString high;
                        QString low;
                        QString ymd;
                        QString week;
                        QString type;
                        high=object.value("high").toString();
                        low=object.value("low").toString();
                        ymd=object.value("ymd").toString();
                        week="昨天";
                        type=object.value("type").toString();
                        yesterday=new forecast(high,low,ymd,week,type);
                        model->setItem(clumns,0,new QStandardItem(ymd));
                        model->setItem(clumns,1,new QStandardItem(week));
                        QIcon *png;
                        if(type=="大雨")
                        {
                            png=new QIcon(":/new/prefix1/bigyu.png");
                        }
                        if(type=="中雨")
                        {
                             png=new QIcon(":/new/prefix1/zhongyu.png");
                        }
                        if(type=="小雨")
                        {
                             png=new QIcon(":/new/prefix1/xiaoyu.png");
                        }
                        if(type=="阴")
                        {
                             png=new QIcon(":/new/prefix1/ying.png");
                        }
                        if(type=="多云")
                        {
                             png=new QIcon(":/new/prefix1/cloud.png");
                        }
                        if(type=="晴")
                        {
                             png=new QIcon(":/new/prefix1/sun.png");
                        }
                        model->setItem(clumns,2,new QStandardItem(*png,type));
                        model->setItem(clumns,3,new QStandardItem(low));
                        model->setItem(clumns,4,new QStandardItem(high));
                        clumns++;
                    }
                    if(json_object_3.contains("forecast"))
                    {
                        QJsonValue json_value_7=json_object_3.value("forecast");
                        QJsonArray json_array=json_value_7.toArray();

                        for(int i=0;i<json_array.size();i++)
                        {
                            QString high;
                            QString low;
                            QString ymd;
                            QString week;
                            QString type;
                            QJsonValue value=json_array.at(i);
                            QJsonObject object=value.toObject();
                            high=object.value("high").toString();
                            low=object.value("low").toString();
                            ymd=object.value("ymd").toString();
                            if(i==0)
                            {
                                week="今天";
                            }
                            if(i==1)
                            {
                                week="明天";
                            }
                            if(i!=0&&i!=1)
                            {
                                week=object.value("week").toString();
                            }
                            type=object.value("type").toString();
                            forecast *day=new forecast(high,low,ymd,week,type);
                            vect.push_back(day);
                            model->setItem(clumns,0,new QStandardItem(ymd));
                            model->setItem(clumns,1,new QStandardItem(week));
                            QIcon *png;
                            if(type=="大雨")
                            {
                                png=new QIcon(":/new/prefix1/bigyu.png");
                            }
                            if(type=="中雨")
                            {
                                 png=new QIcon(":/new/prefix1/zhongyu.png");
                            }
                            if(type=="小雨")
                            {
                                 png=new QIcon(":/new/prefix1/xiaoyu.png");
                            }
                            if(type=="阴")
                            {
                                 png=new QIcon(":/new/prefix1/ying.png");
                            }
                            if(type=="多云")
                            {
                                 png=new QIcon(":/new/prefix1/cloud.png");
                            }
                            if(type=="晴")
                            {
                                 png=new QIcon(":/new/prefix1/sun.png");
                            }
                            model->setItem(clumns,2,new QStandardItem(*png,type));
                            model->setItem(clumns,3,new QStandardItem(low));
                            model->setItem(clumns,4,new QStandardItem(high));
                            sql->addData(forecast(high,low,ymd,week,type));
                            clumns++;
                        }
                    }

                }
            }
        }
        alldata=new weather(time,city,quality,wendu,yesterday,vect);
    }
    else
    {
        qDebug()<<json_error.errorString();
    }
    qDebug()<<alldata->date<<" "<<alldata->city<<" "<<alldata->quality<<" "<<alldata->wendu;
    qDebug()<<yesterday->low<<" "<<yesterday->high<<" "<<yesterday->ymd<<" "<<yesterday->week<<" "<<yesterday->type;
    for(auto it:alldata->fore_15)
    {
        qDebug()<<it->low<<" "<<it->high<<" "<<it->ymd<<" "<<it->week<<" "<<it->type;
    }
}
